from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt
import json


def home(request):
    """主页"""
    return render(request, 'home.html')


@login_required
def game_lobby(request):
    """游戏大厅"""
    return render(request, 'game_lobby.html')


@login_required
def card_collection(request):
    """卡牌收藏"""
    return render(request, 'card_collection.html')


@login_required
def deck_builder(request):
    """卡组构建器"""
    return render(request, 'deck_builder.html')


@login_required
def game_room(request, game_id):
    """游戏房间"""
    # 检查游戏类型，决定使用哪个模板
    from apps.games.models import Game
    try:
        game = Game.objects.get(id=game_id)
        # 如果是AI游戏（player2为None但player2_deck不为None），使用AI对战模板
        if game.player2 is None and game.player2_deck is not None:
            return render(request, 'game_room.html', {'game_id': game_id})
        else:
            # 玩家对战，使用PvP模板
            return render(request, 'pvp_game_room.html', {'game_id': game_id})
    except Game.DoesNotExist:
        # 游戏不存在，返回404
        from django.http import Http404
        raise Http404("游戏不存在")
